<!--
 * @Description: 一卡通管理列表
 * @Author: Li Yujie
 * @Date: 2020/12/22
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2021/01/09
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    一卡通管理
                </div>
                <!--<div class="add-btn">
                    <router-link
                        to="/student/member-poster-list"
                        class="el-button el-button&#45;&#45;primary"
                        tag="el-button"
                    >
                        海报设置
                    </router-link>
                </div>-->
            </div>
            <!-- /header -->
            <el-tabs
                v-model="filterData.status"
                @tab-click="getTableData(1)"
            >
                <el-tab-pane
                    label="全部"
                    name="99"
                />
                <el-tab-pane
                    label="已启用"
                    name="1"
                />
                <el-tab-pane
                    label="未启用"
                    name="2"
                />
            </el-tabs>
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">一卡通信息：</p>
                    <el-input
                        placeholder="请输入一卡通名称或编号"
                        v-model="filterData.title"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">创建时间：</p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->
            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
            >
                <el-table-column
                    label="序号"
                    type="index"
                    align="center"
                    :formatter="emptyFormatter"
                    width="100"
                />
                <el-table-column
                    prop="title"
                    label="一卡通名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="vip_no"
                    label="一卡通编号"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="创建时间"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="toEdit(scope.row.vip_no, 'detail')"
                        >
                            查看
                        </el-button>
                        <el-button
                            type="text"
                            @click="$refs.extensionDialog.show(scope.row)"
                        >
                            推广
                        </el-button>
                        <el-button
                            type="text"
                            @click="toEdit(scope.row.vip_no, 'edit')"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            @click="toRecords(scope.row.vip_no)"
                        >
                            购买记录
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- 分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    :current-page.sync="page.now_page"
                    background
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
            <!-- /分页 -->
        </div>
        <!--推广弹窗-->
        <member-card-extension
            ref="extensionDialog"
        />
    </div>
</template>

<script>
import memberCardExtension from '@/components/member-card/member-card-extension';

export default {
    name: 'MemberCardList',
    data() {
        return {
            filterData: {
                // 状态
                status: '99',
                title: '',
                create_start_time: '',
                create_end_time: '',
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            authResult: {}
        };
    },
    components: {
        memberCardExtension
    },
    mounted() {
        this.init();
    },
    watch: {
        dateTimeRange(value) {
            this.filterData.create_start_time = value ? value[0] / 1000 : '';
            this.filterData.create_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    methods: {
        /**
         * 初始化
         */
        init() {
            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '一卡通管理'}]);
            this.authResult = this.$route.meta.authResult;
            this.getTableData();
        },

        /**
         * @description 获取一卡通管理列表数据
         * @param page
         */
        getTableData(page) {

            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            params.status = Number(params.status);

            this.$post('/student/student_vip%5Cget_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    // this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * 表格数据过滤函数
         * @param row
         * @param column
         * @param val
         * @returns {string|*}
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 跳转编辑页面
         * @param no
         * @param type - add新增 edit编辑 detail查看
         */
        toEdit(no = '', type) {

            if (type === 'edit' && !this.authResult.auth_edit) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$router.push({
                name: 'memberCardEdit',
                query: {
                    no,
                    type
                }
            });
        },

        /**
         * 启用停用（废弃）
         * @param row
         */
        handleModify(row) {
            if (!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm(`确认${row.status === 1 ? '停用' : '启用'}该一卡通吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    vip_no: row.vip_no,
                    status: row.status === 1 ? 0 : 1
                };

                this.$post('', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 跳转购买记录页面
         * @param no
         */
        toRecords(no) {
            this.$router.push({
                name: 'memberCardBuyRecords',
                query: {
                    vip_no: no,
                }
            });
        }
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*el-tabs*/

        /deep/ .el-tabs {

            .el-tabs__nav-wrap {
                height: 54px;
                padding-left: 24px;

                .el-tabs__nav-scroll {
                    .el-tabs__nav {
                        height: 54px;
                        line-height: 54px;
                    }
                }
            }

            .el-tabs__header {
                margin-bottom: 0;
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 24px 24px 0;

            .header-new-box {
                display: flex;
                align-items: center;
                color: #606266;
                white-space: nowrap;
                margin-right: 32px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 32px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    .el-textarea__inner {
        resize: none;
    }
}
</style>
